package com.thx.org.action;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.common.struts2.CurdAction;
import com.thx.common.web.ActionResult;
import com.thx.common.web.WebConstant;
import com.thx.file.util.WebFileUtil;
import com.thx.log.LogUtil;
import com.thx.org.model.Company;
import com.thx.org.service.CompanyManager;
import com.thx.user.model.User;
import com.thx.user.util.ModuleConstant;

/**
 *
 * 建立日期 : 2014年4月24日  下午4:11:49<br>
 * 作者 : shiyan<br>
 * 模块 : <br>
 * 描述 :部门action <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 */
@Namespace("/user")
@Action(value="com", results={
		@Result(name="input", location = "/pages/system/org/editcom.jsp"),
		@Result(name = "json", type = "json", params = { "root", "result" })
})
@Controller
@Scope("prototype")
public class CompanyAction extends CurdAction<Company> {

	private static final long serialVersionUID = 1L;

	protected String uploadFileId;//上传图片对应文件id
	
	@Autowired
	protected CompanyManager companyMgr;

	protected void doInput() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			entity = companyMgr.get(id);
			if(entity==null){
				message = "要修改的记录不存在！！！";
			}
		}else{
			message="不能新增企业！！！";
		}
	}

	protected void doSave() throws Exception {
		try {
			if (StringUtils.isBlank(id)) {
			/*	entity.setComId(null);
				entity.setCreatTime(new Date());
				User user = (User)getSession().getAttribute(WebConstant.KEY_USER);
				entity.setCreatUser(user1.getUserId());
				companyMgr.save(entity);
				message = "添加成功";
				*/
				setResult(ActionResult.FAIL, "ID不能为空");
			} else {
				Company comp = companyMgr.get(id);
				comp.setComName(StringUtils.strip(entity.getComName()));
				comp.setComAddress(StringUtils.strip(entity.getComAddress()));
				if(StringUtils.isNotBlank(uploadFileId)){
					if(StringUtils.isNotBlank(comp.getLogoPath())){
						WebFileUtil.deleteFile(comp.getLogoPath());
					}
					comp.setLogoPath(uploadFileId);
				}
				companyMgr.save(comp);
				setResult(ActionResult.SUCCESS, "修改成功！");
				LogUtil.log(ModuleConstant.ORG_MANAGER, LogUtil.ACTION_UPDATE, LogUtil.RESULT_SUCCESS, "修改公司信息");
			}
		} catch (Exception e) {
				setResult(ActionResult.FAIL, "修改失败！");
			    logger.error(e.getMessage(), e);
		}
	}

	public void checkName() {
		try {
			Company comp = companyMgr.findByName(entity.getComName());
			if (comp == null)
				writeJson("true");
			else{
				if(StringUtils.isNotBlank(id)&&id.equals(comp.getComId())){
					writeJson("true");
				}else{
					writeJson("false");
				}
			}

		} catch (Exception e) {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}
	/**
	 *获取当前用户所在企业信息 
	 * @author Xuejw 2016年10月22日 下午4:34:07
	 */
	public void getCurrentCompany() {
	  User user = (User)getSession().getAttribute(WebConstant.KEY_USER);
	  String json ="";
      try {
          Company comp = companyMgr.get(user.getComId());
          if (comp != null)
              json = com.alibaba.fastjson.JSON.toJSONString(comp);

      } catch (Exception e) {
      }
      try {
        writeJson(json);
      } catch (IOException e) {
      }
  }
	
	public String getUploadFileId() {
		return uploadFileId;
	}

	public void setUploadFileId(String uploadFileId) {
		this.uploadFileId = uploadFileId;
	}

}
